define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte'], function ($, undefined, Backend, Table, Form, Adminlte) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/device/index',
                    add_url: 'device/device/add',
                    edit_url: 'device/device/edit',
                    del_url: 'device/device/del',
                    multi_url: 'device/device/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'app_id', title: __('app id'), operate: 'LIKE'},
                        {field: 'sdk_key', title: __('sdk key'), operate: false},
                        {
                            field: 'expired_at',
                            title: __('Expired'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'contacts', title: __('Contacts'), operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: false},
                        {field: 'count', title: __('Count'), operate: false},
                        {field: 'used_count', title: __('Used Count'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: '查看',
                                text: __('Detail'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'device/key/index'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                commonSearch: true,
                searchFormVisible: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                Form.events.datetimepicker($("form[role=form]"));
            }
        }
    };
    return Controller;
});